package com.fang.study.context;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2021/12/26 7:40 下午
 **/
public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {

    public MyClassPathXmlApplicationContext(String... configLocations) {
        super(configLocations);
    }

    /**
     * refresh() -> prepareRefresh() -> initPropertySources() 增强
     */
    @Override
    protected void initPropertySources() {
        System.out.println("extend initPropertySources method");
        // 由于在AbstractPropertyResolver中会校验该属性在系统参数中是否存在，如果不存在会报message error
        getEnvironment().setRequiredProperties("user.name");
        String requiredProperty = getEnvironment().getRequiredProperty("user.name");
        System.out.println(requiredProperty);
    }

    /**
     * refresh() -> obtainFreshBeanFactory -> refreshBeanFactory -> customizeBeanFactory
     *
     * @param beanFactory beanFactory
     */
    @Override
    protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
        beanFactory.setAllowBeanDefinitionOverriding(false);
        beanFactory.setAllowCircularReferences(false);
        super.customizeBeanFactory(beanFactory);
    }
}
